package com.swing;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * @author zsj
 * @date 2023/9/26 18:35
 */
public class TrangleBulid extends JPanel {


    private int rows = 5; // 金字塔的行数

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        int width = getWidth();
        int height = getHeight();

        int x = width / 2; // 金字塔的中心点横坐标
        int y = height / 2; // 金字塔的中心点纵坐标
        int starSize = 20; // 星号的大小

        for (int i = 1; i <= rows; i++) {
            int startX = x - (i - 1) * starSize / 2;
            int startY = y - (rows - i) * starSize;

            for (int j = 1; j <= 2 * i - 1; j++) {
                int starX = startX + (j - 1) * starSize;
                int starY = startY;
                g.fillRect(starX, starY, starSize, starSize);
            }
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Pyramid");
        TrangleBulid pyramid = new TrangleBulid();
        frame.add(pyramid);
        frame.setSize(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

